package com.edu.bean;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class MyIOC {
    private Map<String, Object> box;

    private Properties pro;

    public MyIOC(String path) {
        box = new HashMap<>();
        init(path);
    }

    public void init(String path){
        InputStream ins = MyIOC.class.getClassLoader().getResourceAsStream("ioc.properties");
        try {
            pro = new Properties();
            pro.load(ins);
            Set<String> strings = pro.stringPropertyNames();
            for (String string : strings) {
                getBean(string);
            }
        } catch (IOException e) {
            System.out.println("没有找到该配置文件");
            e.printStackTrace();
        }
    }

    public Object getBean(String beanId){
        String property = pro.getProperty(beanId);
        try {
            Class clazz = Class.forName(property);
            Object o = clazz.newInstance();
            return o;
        } catch (ClassNotFoundException e) {
            System.out.println("没有找到这个类");
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            System.out.println("无法创建这个对象");
            e.printStackTrace();
        }
        return null;
    }
}
